#ifndef _EVENTLOOP_HH
#define _EVENTLOOP_HH

#include "Acceptor.hh"
#include "TcpConnection.hh"
#include <sys/epoll.h>
#include <errno.h>
#include <error.h>
#include <iostream>
#include <vector>
#include <map>
#include <memory>
#include <functional>

#define MAX_EVENTS 100

using std::vector;
using std::map;
using std::shared_ptr;
using std::function;

class EventLoop 
{
    using fun_ptr = function<void(const shared_ptr<TcpConnection>&)>;
public:
    EventLoop(Acceptor& _acceptor);
    ~EventLoop();
    void Create_epoll_fd();
    void Add_epoll(int fd);
    void Del_epoll(int fd);
    int Wait_epoll(int timeout);
    void New_connection();
    void Old_connection(int fd);
    void Loop();
    void Set_new_conn(const fun_ptr& new_conn);
    void Set_old_conn(const fun_ptr& old_conn);
    void Set_close_conn(const fun_ptr& close_conn);
private:
    int _epoll_fd;
    vector<struct epoll_event> _events;
    Acceptor& _acceptor;
    map<int, shared_ptr<TcpConnection>> _connections;
    fun_ptr _new_conn;
    fun_ptr _old_conn;
    fun_ptr _close_conn;
};

#endif